package com.chujian.framework.sync.aop;

import com.chujian.framework.sync.SyncConst;
import com.chujian.framework.sync.SyncDataDispatcher;
import com.chujian.framework.annotation.sync.EnableSync;
import com.chujian.framework.sync.handler.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.Advisor;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.ComposablePointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.annotation.AnnotationClassFilter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@ConditionalOnProperty(name = "chujian.ctrl.sync.enabled", havingValue = "true", matchIfMissing = true)
@Slf4j
public class SyncConfiguration {

    @Configuration
    @Import({
            SyncMethodAdvice.class, SyncDataDispatcher.class,
            // BaseService
            InsHandler.class, UpdHandler.class, DelHandler.class, DelsHandler.class,
            // mybatis.IService
            SaveHandler.class,
            RemoveByIdHandler.class, RemoveByIdsHandler.class,
            UpdateByIdHandler.class
    })
    public class Config {

    }

    @Bean(name = SyncConst.SYNC_AOP_ADVISER_BEAN)
    public Advisor syncDataAdvisor(SyncMethodAdvice advice) {
        Pointcut pointcut = new ComposablePointcut(
                new AnnotationClassFilter(EnableSync.class),
                new SyncMethodMatcher()
        );
        return new DefaultPointcutAdvisor(pointcut, advice);
    }

}
